package com.ruoyi.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author xumh
 * @title: OrderNumberGenerator
 * @projectName ruoyi
 * @description: TODO
 * @date 2024/12/1215:11
 */
public class OrderNumberGenerator {
    // 单例模式
    private static final OrderNumberGenerator INSTANCE = new OrderNumberGenerator();
    private OrderNumberGenerator() {}

    public static OrderNumberGenerator getInstance() {
        return INSTANCE;
    }

    /**
     * 生成20位订单号
     * 格式：5位随机数 + yyMMddHHmmssSSS
     * @return 订单号
     */
    public String generateOrderNumber() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
        String timestamp = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = random.nextInt(90000) + 10000; // 生成5位随机数
        return randomNumber+timestamp;
    }

    public static void main(String[] args) {
        System.out.println("生成20位订单号: "+OrderNumberGenerator.getInstance().generateOrderNumber());
    }
}
